<?php
declare (strict_types = 1);

namespace app\controller;

use app\common\ApiResponse;
use \app\service\AccountRecordService;
use app\BaseController;
use think\Request;

class AccountRecordController extends BaseController
{
    private $accountRecordService;

    public function __construct()
    {
        $app = app(); // 获取当前应用的实例
        $this->accountRecordService = new AccountRecordService($app);
    }
    /**
     * 查询账单记录列表
     *
     * @return \think\Response
     */
    public function getList()
    {
        $data = input();
        if(empty($data['user_id']) || empty($data['acc_id']) ){
            return json("缺少参数user_id|acc_id");
        }
        $list = $this->accountRecordService->getList($data);
        return ApiResponse::successList($list, "查询成功");

    }

    /**
     * 添加账单记录
     *
     * @return \think\Response
     */
    public function create()
    {
        $data = input();
        if(empty($data['record_name'] ) || empty($data['user_id'])|| empty($data['acc_id'])|| empty($data['cate_id'])|| empty($data['record_price'])){
            return json("缺少参数record_name|user_id|acc_id|cate_id|record_price");
        }
        $res = $this->accountRecordService->create($data);
        return ApiResponse::success($res, "添加成功");
    }

    /**
     * 修改账单记录
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function update()
    {
        $data = input();
        if(empty($data['record_id'] ) ||empty($data['record_name'] ) || empty($data['user_id'])|| empty($data['acc_id'])|| empty($data['record_price'])){
            return json("缺少参数record_id|record_name|user_id|acc_id|record_price");
        }
        $res = $this->accountRecordService->update($data);
        return ApiResponse::success($res, "修改成功");
    }

    /**
     * 查询账单记录
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function getOne()
    {
        $data = input();
        if(empty($data['record_id'] )){
            return json("缺少参数record_id");
        }
        $res = $this->accountRecordService->getOne($data);
        return ApiResponse::success($res, "获取成功");
    }


    /**
     * 删除账单记录
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete()
    {
        $data = input();
        if(empty($data['record_id'] )){
            return json("缺少参数record_id");
        }
        $res = $this->accountRecordService->delete($data);
        return ApiResponse::success($res, "删除成功");
    }

}
